class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        unordered_map<int, int> mmap;
        int ret = 0;
        for (int left = 0, right = 0; right < fruits.size(); right++)
        {
            mmap[fruits[right]]++;
            while (mmap.size() > 2)
            {
                mmap[fruits[left]]--;
                if (mmap[fruits[left]] == 0)
                    mmap.erase(fruits[left]);
                left++;
            }
            ret = max(ret, right - left + 1);

        }
        return ret;
    }
};